using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace FederationMembershipDB
{
	public class TransactionDetails
	{
		DateTime _paymentDate;
		string _receivedFrom;
		double _total;
		string _paymentMethod;
        int _memberID;
        int _chequeID;

        public DateTime PaymentDate
        {
            get { return _paymentDate; }
            set { _paymentDate = value; }
        }
        public string ReceivedFrom
        { get { return _receivedFrom; } set { _receivedFrom = value; } }
        public double Total
        { get { return _total; } set { _total = value; } }
        public string PaymentMethod
        { get { return _paymentMethod; } set { _paymentMethod = value; } }
        public int MemberID
        { get { return _memberID; } set { _memberID = value; } }
        public int ChequeID
        { get { return _chequeID; } set { _chequeID = value; } }

        public void AddTransaction(ref int tranDetailsID)
		{
            //Parameter: ref int ChequeID - when cheque ID is set, the int variable passed in will be updated in the calling function

            //           SqlTransaction - to keep the transaction as a whole unit, pass in an existing Transaction

            //Get the last ID in the DB
            
            string sqlSelectID = "SELECT TOP(1) ID FROM TransactionDetails Order By ID DESC;";
            DataSet ds = Utilities.SearchDatabase(sqlSelectID);

            //check to see if it will be the first item inserted.
            if (ds.Tables[0].Rows.Count == 0)
                tranDetailsID = 0;            
            else
                tranDetailsID = int.Parse(ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString()) + 1; // +1 to increment - the next chequeID
            
            //Construct INSERT statement
            string sqlInsert = "INSERT INTO TransactionDetails VALUES ('" + tranDetailsID + "', CONVERT(datetime, '" + PaymentDate + "', 103),'" + Total + "','" + PaymentMethod + "','" + ChequeID + "','" + MemberID + "');";

            Utilities.DatabaseNonQuery(sqlInsert);
		}
	}
}
